

package cn.iocoder.yudao.module.apijson.framework;

import apijson.NotNull;
import apijson.orm.AbstractSQLExecutor;
import apijson.orm.SQLConfig;
import cn.iocoder.yudao.framework.common.util.spring.SpringUtils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.Map;


/**
 * executor for query(read) or update(write) MySQL database
 *
 * @author Lemon
 */
@Slf4j
public class APIJSONSQLExecutor extends AbstractSQLExecutor<Long, JSONObject, JSONArray> {
    public static final String TAG = "APIJSONSQLExecutor";

    static {
        try { //加载驱动程序
            log.info("尝试加载 MySQL 8 驱动 <<<<<<<<<<<<<<<<<<<<< ");
            Class.forName("com.mysql.cj.jdbc.Driver");
            log.info("成功加载 MySQL 8 驱动！>>>>>>>>>>>>>>>>>>>>>");
        } catch (ClassNotFoundException e) {
            log.error("加载 MySQL 8 驱动失败，请检查 pom.xml 中 mysql-connector-java 版本是否存在以及可用 ！！！");
            log.error(e.getMessage());
            try { //加载驱动程序
                log.info("尝试加载 MySQL 7 及以下版本的 驱动 <<<<<<<<<<<<<<<<<<<<< ");
                Class.forName("com.mysql.jdbc.Driver");
                log.info("成功加载 MySQL 7 及以下版本的 驱动！>>>>>>>>>>>>>>>>>>>>>");
            } catch (ClassNotFoundException e2) {
                log.error("加载 MySQL 7 及以下版本的 驱动失败，请检查 pom.xml 中 mysql-connector-java 版本是否存在以及可用 ！！！");
                log.error(e2.getMessage());
            }
        }

    }
    @Override
    public PreparedStatement setArgument(@NotNull SQLConfig<Long, JSONObject, JSONArray> config, @NotNull PreparedStatement statement, int index, Object value) throws SQLException {
        return super.setArgument(config, statement, index, value);
    }


    @Override
    protected Object getValue(
            SQLConfig<Long, JSONObject, JSONArray> config, ResultSet rs, ResultSetMetaData rsmd, int row
            , JSONObject table, int columnIndex, String label, Map<String, JSONObject> childMap, Map<String, String> keyMap
    ) throws Exception {
        return super.getValue(config, rs, rsmd, row, table, columnIndex, label, childMap, keyMap);
    }

    // 支持 !key 反选字段 和 字段名映射，依赖插件 https://github.com/APIJSON/apijson-column
    @Override
    protected String getKey(
            SQLConfig<Long, JSONObject, JSONArray> config, ResultSet rs, ResultSetMetaData rsmd, int row
            , JSONObject table, int columnIndex, Map<String, JSONObject> childMap, Map<String, String> keyMap
    ) throws Exception {

        String key = super.getKey(config, rs, rsmd, row, table, columnIndex, childMap, keyMap);
        if (APIJSONSQLConfig.ENABLE_COLUMN_CONFIG) {
            return ColumnUtil.compatOutputKey(key, config.getTable(), config.getMethod());
        }

        return key;
    }


    // 适配连接池，如果这里能拿到连接池的有效 Connection，则 SQLConfig 不需要配置 dbVersion, dbUri, dbAccount, dbPassword
    @Override
    public Connection getConnection(SQLConfig<Long, JSONObject, JSONArray> config) throws Exception {
        String key =getConnectionKey(config);
        Connection c = connectionMap.get(key);
        System.out.println(connectionMap);
        if (c == null || c.isClosed()) {
            //获取连接
            DruidDataSource bean = SpringUtils.getApplicationContext().getBean("masterDataSource",DruidDataSource.class);
            connectionMap.put(key, bean.getConnection());
        }

        return super.getConnection(config);
    }
}
